package com.gwcm.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gwcm.exception.BusinessException;
import com.gwcm.mapper.CategoryMapper;
import com.gwcm.model.dto.category.CategoryQueryRequest;
import com.gwcm.model.entity.Category;
import com.gwcm.model.vo.CategoryVo;
import com.gwcm.service.CategoryService;
import com.gwcm.util.PageUtil;
import com.yhz.commonutil.common.ErrorCode;
import com.yhz.commonutil.constant.SortConstant;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author 15375
* @description 针对表【category】的数据库操作Service实现
* @createDate 2023-03-11 15:41:21
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService {
    @Resource
    private CategoryMapper categoryMapper;
    /**
     * 产品类别分页查询
     * @param categoryQueryRequest 查询条件
     * @return
     * @throws BusinessException
     */
    @Override
    public IPage<CategoryVo> pageList(CategoryQueryRequest categoryQueryRequest) throws BusinessException {
        IPage<Category> page = PageUtil.vaildPageParam(categoryQueryRequest.getCurrent(), categoryQueryRequest.getPageSize());
        String sortField = categoryQueryRequest.getSortField();
        String sortOrder = categoryQueryRequest.getSortOrder();

        if (StrUtil.isBlank(sortOrder) || !StrUtil.equalsAny(sortOrder, SortConstant.SORT_ORDER_ASC, SortConstant.SORT_ORDER_DESC)) {
            //默认按升序排序
            sortOrder = SortConstant.SORT_ORDER_ASC;
        }
        categoryQueryRequest.setSortField(sortField);
        categoryQueryRequest.setSortOrder(sortOrder);
        IPage<CategoryVo> categoryToIPage=categoryMapper.selectCategoryToPage(page, categoryQueryRequest);
        IPage<CategoryVo> categoryVoIPage = categoryToIPage.convert(item -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(item, categoryVo);
            return categoryVo;
        });

        return categoryVoIPage;
    }

    /**
     * 新增产品类别
     * @param category
     */
    @Override
    public void saveCategory(Category category) {
        String categoryName = category.getCategoryName();
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.eq("category_name", categoryName);
        Category selectOne = baseMapper.selectOne(categoryQueryWrapper);
        if (!ObjectUtil.isEmpty(selectOne)) {
            throw new BusinessException("类别名称已存在", 50000, "");
        }
        int insert = baseMapper.insert(category);
        if (insert < 1) {
            throw new BusinessException(ErrorCode.SAVE_ERROR);
        }
    }

    /**
     * 修改产品类别
     * @param category
     */
    @Override
    public void updateCategory(Category category) {
        String categoryName = category.getCategoryName();
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.eq("category_name",categoryName);
        Category selectOne = baseMapper.selectOne(categoryQueryWrapper);
        if(!ObjectUtil.isEmpty(selectOne)){
            throw new BusinessException("类别名称已存在", 50000, "");
        }
        int update = baseMapper.updateById(category);
        if (update < 1) {
            throw new BusinessException(ErrorCode.UPDATE_ERROR);
        }
    }

}




